package com.region.integral.business_user.obtain.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.business_user.obtain.dao.IIntegralGoodsConfirmed;
import com.region.integral.business_user.pojo.BUIEntity;
import com.region.integral.business_user.xml.Regulation;
import com.region.integral.business_user.xml.dto.GoodsConfirmed;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.BusinessUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iIntegralGoodsConfirmed")
public class IntegralGoodsConfirmedImpl extends BaseHibernate implements IIntegralGoodsConfirmed {

	@Override
	public void goodsConfirmed(int userid, int minute, IntegralData integralData)
			throws IntegralException {
		BUIEntity buiEntity = getBUIEntity(userid);
		if (buiEntity != null) {
			Regulation regulation = XMLRegulationUtil.unmarshaller2();
			List<GoodsConfirmed> goodsConfirmeds = regulation.getGoodsconfirmed();
			for (GoodsConfirmed goodsConfirmed : goodsConfirmeds) {
				if(goodsConfirmed.getBelowOne()!=0&&goodsConfirmed.getBelowOne()>=minute){
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ goodsConfirmed.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid,goodsConfirmed.getIntegral(), "goodsConfirmed"));
					break;
				}
				if (goodsConfirmed.getBelowTwo()!=0&&goodsConfirmed.getBelowTwo()>=minute) {
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ goodsConfirmed.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid,goodsConfirmed.getIntegral(), "goodsConfirmed"));
				}
			}
		}
		
		
	}

}
